/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amuthu.expression.function.indicator;

import org.amuthu.DataStore;
import org.amuthu.expression.Expression;
import org.amuthu.indicator.PercentagePriceOscillator;
import org.amuthu.parser.ExpressionVisitor;

/**
 * ppo(fastPeriod, slowPeriod,startOffset)
 * @author prabhu
 */
public class PPOFunction extends BaseIndicatorFunction
{
   private int fastPeriod, slowPeriod;

   public PPOFunction()
   {
   }

   @Override
   public double calculateIndicator(DataStore dataStore, Expression[] arguments, ExpressionVisitor visitor)
   {
      PercentagePriceOscillator ppo = new PercentagePriceOscillator(dataStore, fastPeriod, slowPeriod, getOffset());

      return ppo.calculate();
   }

   @Override
   public int calculateDependency(DataStore dataStore, Expression[] arguments, ExpressionVisitor visitor)
   {
      PercentagePriceOscillator ppo = new PercentagePriceOscillator(dataStore, fastPeriod, slowPeriod, getOffset());

      return ppo.getDayDependency();
   }

   @Override
   public boolean doUpdateParameters(DataStore dataStore, Expression[] arguments, ExpressionVisitor visitor)
   {
      if (arguments.length < 2)
      {
         return false;
      }

      fastPeriod = (int) visitor.evaluate(arguments[0]);

      if (fastPeriod < 0)
      {
         return false;
      }

      slowPeriod = (int) visitor.evaluate(arguments[1]);

      if (slowPeriod < 0)
      {
         return false;
      }

      int startOffset = (arguments.length > 2) ? (int) visitor.evaluate(arguments[2]) : 0;

      setOffset(startOffset);

      return true;
   }

}
